<!-- 图片上传 -->
<template>
  <div>
    <div class="Upload">
      <template v-if="disabled">
        <el-image
          :src="item.url"
          class="img"
          alt=""
          v-for="item in showList"
          :key="item.url"
          :preview-src-list="[item.url]"
        ></el-image>
      </template>

      <template v-else>
        <!-- 上传一张 -->
        <template v-if="one">
          <div v-if="imageUrl" class="img action">
            <img :src="imageUrl" class="img" />
            <label class="label">
              <i class="success el-icon-check"></i>
            </label>
            <div class="action_item flex align-center justify-center">
              <i class="el-icon-zoom-in hover" @click="preview"></i>
              <i
                class="el-icon-delete hover"
                style="margin-left: 15px"
                @click="remove"
              ></i>
            </div>
          </div>

          <el-upload
            class="avatar-uploader"
            v-else
            :action="uploadAction"
            :headers="headers"
            :show-file-list="false"
            :on-success="handleSuccess"
          >
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </template>

        <!-- 多张上传 -->
        <template v-else>
          <el-upload
            ref="upload"
            :action="uploadAction"
            :headers="headers"
            list-type="picture-card"
            :file-list="fileList"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </template>

        <el-dialog :visible.sync="dialogVisible" append-to-body>
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </template>
    </div>
  </div>
</template>

<script>
export default {
  name: "Upload",
  props: {
    // 展示的数据
    value: {
      type: String,
      default: "",
    },
    // 是否上传
    disabled: {
      type: Boolean,
      default: false,
    },
    // 只上传一张
    one: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    const DT_TOKEN = this.$store.state.user.token;
    return {
      uploadAction: this.$config.url + "/sys/oss/upload",
      headers: { DT_TOKEN },

      showList: [], // 图片预览
      fileList: [], // 多图数据
      imageUrl: "", // 单图数据

      // 上传的图片预留
      dialogImageUrl: "",
      dialogVisible: false,

      flag: false, // 阻止双向数据绑定对fileList带来的影响
    };
  },
  watch: {
    value: {
      handler(n) {
        this.splitData(n);
      },
      immediate: true, // 页面加载触发
    },
  },
  mounted() {},
  methods: {
    splitData(value) {
      if (value) {
        this.showList = value.split(",").map((item) => {
          return {
            url: item,
          };
        });
      }

      if (this.one) {
        this.imageUrl = value;
      } else {
        if (this.flag) return;
        this.flag = true;

        this.fileList = this.showList;
      }
    },

    // 单图图片预览
    preview() {
      this.dialogImageUrl = this.imageUrl;
      this.dialogVisible = true;
    },
    // 删除单张图片
    remove() {
      this.emit([]);
    },

    // 多图图片预览
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 多图片删除
    handleRemove(file, fileList) {
      this.emit(fileList);
    },

    // 图片上传成功
    handleSuccess(response, file, fileList) {
      this.emit(fileList);
    },
    // 事件回调
    emit(fileList) {
      let obj = this.arr2string(fileList);
      this.$emit("input", obj.str);
      this.$emit("success", obj.arr);
    },
    // arr2string
    arr2string(fileList) {
      let arr = fileList.map((item) => {
        if (item.status === "success") {
          return item?.response?.url || item.url;
        }
      });
      return {
        str: arr.join(","),
        arr: arr,
      };
    },
  },
};
</script>
<style lang='scss' scoped>
.Upload {
  .avatar-uploader {
    ::v-deep .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    ::v-deep .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 148px;
      height: 148px;
      line-height: 148px;
      text-align: center;
    }
  }

  .img {
    width: 148px;
    height: 148px;
    border-radius: 6px;
    margin: 0 8px 8px 0;
    overflow: hidden;
    border: 1px solid #c0ccda;
    &:not(:last-child) {
      margin-right: 8rpx;
    }
  }
  .action {
    position: relative;

    &:hover .label {
      display: none;
    }
    &:hover .action_item {
      opacity: 1;
    }

    .label {
      position: absolute;
      right: -17px;
      top: -7px;
      width: 46px;
      height: 26px;
      background: #13ce66;
      text-align: center;
      transform: rotate(45deg);
      box-shadow: 0 0 1pc 1px rgb(0 0 0 / 20%);

      .success {
        color: #fff;
        font-size: 12px;
        margin-top: 12px;
        transform: rotate(-45deg);
      }
    }

    .action_item {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      transition: opacity 0.3s;
    }
  }
}
</style>